
// Source File Name:   SET_CSImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.IVL_CS;
import com.sunwayhorizo.health.hl7.datatypes.SET_CS;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


//            SETCommonImpl, ANYImpl, BLImpl, CSImpl,
//            STImpl, TYPEImpl

public class SET_CSImpl extends SETCommonImpl
    implements SET_CS
{

    public static final String RCS_ID = "$Header: SET_CSImpl.java 1.0 2006/09/16 09:22:44 amchanda noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: SET_CSImpl.java 1.0 2006/09/16 09:22:44 amchanda noship $", "com.sunwayhorizo.health.hl7.datatypes.common");
    private static final SET_CS nullNI = new SET_CSImpl(CSImpl.valueOf("NI"));
    private static final SET_CS nullOTH = new SET_CSImpl(CSImpl.valueOf("OTH"));
    private static final SET_CS nullNINF = new SET_CSImpl(CSImpl.valueOf("NINF"));
    private static final SET_CS nullPINF = new SET_CSImpl(CSImpl.valueOf("PINF"));
    private static final SET_CS nullUNK = new SET_CSImpl(CSImpl.valueOf("UNK"));
    private static final SET_CS nullASKU = new SET_CSImpl(CSImpl.valueOf("ASKU"));
    private static final SET_CS nullNAV = new SET_CSImpl(CSImpl.valueOf("NAV"));
    private static final SET_CS nullMSK = new SET_CSImpl(CSImpl.valueOf("MSK"));
    private static final SET_CS nullNA = new SET_CSImpl(CSImpl.valueOf("NA"));
    private static final SET_CS nullNASK = new SET_CSImpl(CSImpl.valueOf("NASK"));
    
    private static ST _flag;

    public static SET_CS valueOf(CS acs[])
    {
        return new SET_CSImpl(acs);
    }

    public static SET_CS valueOf(CS cs)
    {
        if(cs == null || cs.isNull().isTrue())
            return null;
        else
            return new SET_CSImpl(new CS[] {
                cs
            });
    }
    
    public static SET_CS valueOf(CS cs,ST flag)
    {
        if(cs == null || cs.isNull().isTrue())
            return null;
        else
            return new SET_CSImpl(new CS[] {
                cs
            },flag);
    }

    public static SET_CS valueOf(Set set)
    {
        return new SET_CSImpl(set);
    }

    public static SET_CS nullSET_CS()
    {
        return new SET_CSImpl(((CS) (null)));
    }

    public SET_CSImpl(CS acs[])
    {
        if(acs == null)
            return ;
        for(int i = 0; i < acs.length; i++)
            if(acs[i] != null && acs[i].isNull().isFalse())
                mSet.add(acs[i]);

    }
    
    public SET_CSImpl(CS acs[],ST flag)
    {
        if(acs == null)
            return ;
        for(int i = 0; i < acs.length; i++)
            if(acs[i] != null && acs[i].isNull().isFalse())
                mSet.add(acs[i]);
        _flag = flag;

    }

    public SET_CSImpl(Set set)
    {
        super(set);
    }

    public SET_CSImpl(CS cs)
    {
        super(cs);
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.SET_CS;
    }

    public BL contains(CS cs)
    {
        return BLImpl.valueOf(mSet.contains(cs));
    }

    public BL contains(SET_CS set_cs)
    {
        if(isNull().isTrue())
            return BLImpl.FALSE;
        else
            return BLImpl.valueOf(mSet.containsAll(set_cs.toSet()));
    }

    public SET_CS union(SET_CS set_cs)
    {
        if(isNull().isTrue())
            if(set_cs.isNull().isTrue())
                return this;
            else
                return set_cs;
        if(set_cs.isNull().isTrue())
        {
            return this;
        } else
        {
            HashSet hashset = new HashSet(mSet);
            hashset.addAll(set_cs.toSet());
            return valueOf(hashset);
        }
    }

    public SET_CS union(CS cs)
    {
        if(cs.isNull().isTrue())
        {
            return this;
        } else
        {
            HashSet hashset = new HashSet(mSet);
            hashset.add(cs);
            return new SET_CSImpl(hashset);
        }
    }

    public SET_CS except(CS cs)
    {
        if(isNull().isTrue())
        {
            return this;
        } else
        {
            HashSet hashset = new HashSet(mSet);
            hashset.remove(cs);
            return new SET_CSImpl(hashset);
        }
    }

    public SET_CS except(SET_CS set_cs)
    {
        if(isNull().or(set_cs.isNull()).isTrue())
        {
            return this;
        } else
        {
            HashSet hashset = new HashSet(mSet);
            Set set = set_cs.toSet();
            hashset.removeAll(set);
            return new SET_CSImpl(hashset);
        }
    }

    public SET_CS intersection(SET_CS set_cs)
    {
        if(isNull().or(set_cs.isNull()).isTrue())
            return nullSET_CS();
        HashSet hashset = new HashSet();
        for(Iterator iterator = set_cs.toSet().iterator(); iterator.hasNext();)
        {
            CS cs = (CS)iterator.next();
            if(mSet.contains(cs))
                hashset.add(cs);
        }

        return valueOf(hashset);
    }

    public SET_CS promotion(CS cs)
    {
        HashSet hashset = new HashSet();
        hashset.add(cs);
        return valueOf(hashset);
    }

    public BL equal(ANY any)
    {
        if(!(any instanceof SET_CS))
            return BLImpl.FALSE;
        if(isNull().or(any.isNull()).isTrue())
        {
            return BLImpl.FALSE;
        } else
        {
            SET_CS set_cs = (SET_CS)any;
            return BLImpl.valueOf(equalsSet(set_cs.toSet()));
        }
    }

    public IVL_CS hull()
    {
        throw ANYImpl.newUnsupportedException("SET<CS>", "hull");
    }

    public ST literal()
    {
        StringBuffer stringbuffer = new StringBuffer("{");
        for(Iterator iterator = mSet.iterator(); iterator.hasNext();)
        {
            CS cs = (CS)iterator.next();
            stringbuffer.append(cs.literal().toString());
            if(iterator.hasNext())
                stringbuffer.append("; ");
        }

        stringbuffer.append("}");
        return STImpl.valueOf(stringbuffer.toString());
    }

    protected boolean verifyItem(Object obj)
    {
        return (obj instanceof CS) && ((CS)obj).nonNull().isTrue();
    }

    public int hashCode()
    {
        return super.hashCode();
    }

    public static SET_CS nullSET_CS(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

    @Override
    public ST flag()
    {
        return _flag;
    }

}
